@extends('layouts.default')

@section('title', '教师信息')

@push('scripts')
<script>
    var filters = {};
    var per_page = 10;
    var totalNum = 0;
    layui.use(['layer','form','table','laytpl','laydate','element','laypage','dropdown'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var dropdown = layui.dropdown;

        renderSelect();

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            getList(1, per_page)
        });
        form.on('submit(exportBtn)', function(data) {
            var param = data.field;
            var para = {
                "search": param,
                "form_key": "TEACHERTRAINING_ACTIVITY_TABLE"
            };

            exportTeacher(para, function(res) {
                if (res.code == '9') {
                    layer.msg(res.msg, {icon: 5, shift: 6});
                } else {
                    exportsDel('教师档案袋信息列表.xlsx', '/download/excel/'+res.data.file_name);
                }
            });
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }

            var para = {
                "page": page,
                "search": filters,
                "limit": limit,
            };

            getTeacherList(para, function(res){
                if(res.code === 1) {
                    if(totalNum != res.total) {
                        totalNum = res.total;
                        per_page = res.limit;
                        renderPageBar(page,per_page)
                    }

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'nob',
                        cols: [[
                            {type: 'checkbox'},
                            {field: 'major_subject_name', title: '主学科', align: 'center', templet: function(d) {
                                return d.major_subject_array.length > 0 ? d.major_subject_array[0].selectValue : '-';
                            }},
                            {field: 'name', title: '教师姓名', align: 'center'},
                            {field: 'gender_value', title: '性别', align: 'center', templet: function(d) {
                                return d.gender_set.length > 0 ? d.gender_set[0].selectValue : '-';
                            }},
                            {field: 'title', title: '职称', align: 'center', templet: function(d) {
                                let ti = d.title_info ? d.title_info : '';
                                if(ti) {
                                    return ti.tea_title ? ti.title_set[0].selectValue : '-';
                                } else {
                                    return '-';
                                }
                            }},
                            {field: 'rank', title: '岗位等级', align: 'center', templet: function(d) {
                                let ti = d.title_info ? d.title_info : '';
                                if(ti) {
                                    return ti.tea_title_rank ? ti.rank_set[0].selectValue : '-';
                                } else {
                                    return '-';
                                }
                            }},
                            {field: 'teaching_year', title: '教龄', align: 'center', templet: function(d) {
                                let isHq = d.last_group_id == 11800 ? true : false;
                                if(isHq) {
                                    return '-';
                                } else {
                                    return  d.teaching_year ? d.teaching_year : 0;
                                }
                            }},
                            {field: 'work_year', title: '工龄', align: 'center'},
                            {field: 'academic', title: '学历', align: 'center', templet: function(d) {
                                let gs = d.graduated_school ? d.graduated_school : '';
                                if(gs) {
                                    return gs.academic ? gs.academic_set[0].selectValue : '-';
                                } else {
                                    return '-';
                                }
                            }},
                            {field: 'zzmm', title: '政治面貌', align: 'center', templet: function(d) {
                                return d.political_set.length > 0 ? d.political_set[0].selectValue : '-';
                            }},
                            {field: 'age', title: '年龄', align: 'center'},
                            {title: '操作', align: 'center', toolbar: '#opDemo'}
                        ]],
                        id:"tableIns",
                        done: function(res, curr, count) {
                            setBtnPermission();
                            curr_page = page;

                            var options = this;
                            // 获取当前行数据
                            table.getRowData = function(elem){
                                var index = $(elem).closest('tr').data('index');
                                return table.cache[options.id][index] || {};
                            };

                            $('.assess-selector').on('change', function(){
                                var value = this.value; // 获取选中项 value
                                var data = table.getRowData(this); // 获取当前行数据(如 id 等字段，以作为数据修改的索引)
                                // 更新数据中对应的字段
                                // data.city = value;
                                // 显示 - 仅用于演示
                                // layer.msg('选中值: '+ value +'<br>当前行数据：'+ JSON.stringify(data));
                                // console.log(value, data)
                                toUpdateUserAssessment(data.id, value);
                            });
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                limits:[10, 50, 100, 150, 200, 300],
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        function exportsDel(showName,fileName){
            window.location.href = "{{ route('download.FileDownloadDel') }}?filename="+showName+"&file_url="+fileName;
        }

        //更新考核情况
        function toUpdateUserAssessment(id, assess) {
            updateUserAssessment({id: id, assess_value: assess}, function(res) {
                if (res.code == '9') {
                    layer.msg(res.msg, {icon: 5, shift: 6});
                } else {
                    layer.msg('更新成功', {icon: 6,time: 1000});
                }
            });
        }

        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;

            if(obj.event === 'tip'){
                var index = layer.load(2);
                // todo
                layer.msg('用户缺失uac身份证信息，请完善用户基本信息！', {icon: 5,time: 3000});
                layer.close(index);
            }
        });
        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'assessUpdateBtn':
                    //勾选数据
                    var checkStatus = table.checkStatus(obj.config.id).data;

                    if(checkStatus.length == 0){
                        layer.msg('请选择需要操作的数据！');return false;
                    }

                    var ids = [];
                    for(var i=0;i<checkStatus.length;i++){
                        ids.push(checkStatus[i].id)
                    }
                    ids = ids.join(',');

                    var index = layer.load(2);
                    layer.msg('批量更新为：', {
                        time: 0 //不自动关闭
                        ,btn: ['优秀', '优秀-记功', '优秀-嘉奖', '合格', '基本合格', '不合格', '未定等次', '不参加考核']
                        ,yes: function(index){
                            toUpdateUsersAssessment(ids, '优秀');
                            // layer.close(index);
                        },btn2: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '记功');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        },btn3: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '嘉奖');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        },btn4: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '合格');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        },btn5: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '基本合格');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        },btn6: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '不合格');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        },btn7: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '未定等次');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        },btn8: function(index, layero){
                            //按钮【按钮二】的回调
                            toUpdateUsersAssessment(ids, '不考核');
                            // return false; //开启该代码可禁止点击该按钮关闭
                        }
                    });
                    layer.close(index);
                break;
            };
        });

        //更新考核情况
        function toUpdateUsersAssessment(ids, assess) {
            updateUsersAssessment({ids: ids, assess_value: assess}, function(res) {
                if (res.code == '9') {
                    layer.msg(res.msg, {icon: 5, shift: 6});
                } else {
                    layer.msg('更新成功', {icon: 6,time: 2000}, function() {
                        getList(1, per_page);
                        layer.closeAll();
                    });
                }
            });
        }

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            getList(1, per_page);
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['SUBJECT', 'gender', 'academic', 'title_teacher', 'title_teacher_rank']}, function (res){
                    if(res.code === 1) {
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];

                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }

                        form.render();

                        resolve();
                    }
                });
            });
        }



    });
</script>
@endpush

@section('content')
<style>
    .layui-layer-btn a {
        width: 100px;
        text-align: center;
        color: #fff;
        border-color: #2d8cf0;
        background-color: #2d8cf0!important;
    }
    .layui-layer-btn{
        display: grid;
        grid-template-columns: 25% 25% 25% 25%;
    }
</style>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <form id="qryForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">主学科：</label>
                        <div class="layui-input-inline">
                            <select name="subject" lay-filter="subjectList" data-role="subjectList"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">教师姓名：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" placeholder="请选择" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">职称：</label>
                        <div class="layui-input-inline">
                            <select name="title_teacher" lay-filter="title_teacherList" data-role="title_teacherList"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">学历：</label>
                        <div class="layui-input-inline">
                            <select name="academic" lay-filter="academicList" data-role="academicList"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">性别：</label>
                        <div class="layui-input-inline">
                            <select name="gender" lay-filter="genderList" data-role="genderList"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">岗位等级：</label>
                        <div class="layui-input-inline">
                            <select name="title_teacher_rank" lay-filter="title_teacher_rankList" data-role="title_teacher_rankList"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">教龄：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="age" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" lay-submit lay-filter="queryBtn" class="layui-btn layui-btn-normal">查询</button>
                        <button type="button" lay-submit lay-filter="resetBtn" class="layui-btn layui-btn-primary">重置</button>
                        <button btnPermission="357" type="button" lay-submit lay-filter="exportBtn" class="layui-btn layui-btn-normal">导出</button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>

<div class="layui-card">
    <script type="text/html" id="toolbarHead">
        <div class="list-head">
            <h3>教师列表</h3>
            <!--
            <div class="layui-btn-container list-opt-btns">
                <a class="layui-btn layui-btn-normal" id="assess-update-btn" lay-event="assessUpdateBtn"> 考核情况更新 </a>
            </div>-->
        </div>
    </script>

    <div class="layui-card-body">
        <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
        <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
    </div>

    <script type="text/html" id="TPL-select-c">
        <!-- <select name="assess_info" class="layui-border assess-selector" lay-ignore>
            @{{#  if(d.assessment.length > 0 && d.assessment[0].situation_set.length > 0){ }}
                @{{#  if(d.assessment[0].situation_set[0].selectValue == '优秀'){ }}
                <option value="优秀" selected='selected'>优秀</option>
                <option value="合格">合格</option>
                @{{#  } else if(d.assessment[0].situation_set[0].selectValue == '合格'){ }}
                <option value="合格" selected='selected'>合格</option>
                <option value="优秀">优秀</option>
                @{{#  } else { }}
                <option value="合格">合格</option>
                <option value="优秀">优秀</option>
                <option value="@{{ d.assessment[0].situation_set[0].selectValue}}" selected='selected'>@{{ d.assessment[0].situation_set[0].selectValue }}</option>
                @{{#  } }}
            @{{#  } else { }}
            <option value="合格" selected='selected'>合格</option>
            <option value="优秀">优秀</option>
            @{{#  } }}
        </select>  -->
        <select name="assess_info" class="layui-border assess-selector" lay-ignore>
            @{{#  if(d.last_group_id == 11800){ }}
                @{{#  if(d.assessment.length > 0 && d.assessment[0].situation_set.length > 0){ }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '优秀'){ }}
                    <option value="优秀" selected='selected'>优秀</option>
                    @{{#  } else { }}
                    <option value="优秀">优秀</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '记功'){ }}
                    <option value="记功" selected='selected'>优秀-记功</option>
                    @{{#  } else { }}
                    <option value="记功">优秀-记功</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '嘉奖'){ }}
                    <option value="嘉奖" selected='selected'>优秀-嘉奖</option>
                    @{{#  } else { }}
                    <option value="嘉奖">优秀-嘉奖</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '合格'){ }}
                    <option value="合格" selected='selected'>合格</option>
                    @{{#  } else { }}
                    <option value="合格">合格</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '基本合格'){ }}
                    <option value="基本合格" selected='selected'>基本合格</option>
                    @{{#  } else { }}
                    <option value="基本合格">基本合格</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '不合格'){ }}
                    <option value="不合格" selected='selected'>不合格</option>
                    @{{#  } else { }}
                    <option value="不合格">不合格</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '未定等次'){ }}
                    <option value="未定等次" selected='selected'>未定等次</option>
                    @{{#  } else { }}
                    <option value="未定等次">未定等次</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '不考核'){ }}
                    <option value="不考核" selected='selected'>不参加考核</option>
                    @{{#  } else { }}
                    <option value="不考核">不参加考核</option>
                    @{{#  } }}
                @{{#  } else { }}
                    <!-- 优秀-记功, 优秀-嘉奖, 合格, 基本合格, 不合格, 未定等次, 不参加考核（长病假*4） -->
                    <option value="合格" selected='selected'>合格</option>
                    <option value="优秀">优秀</option>
                    <option value="记功">优秀-记功</option>
                    <option value="嘉奖">优秀-嘉奖</option>
                    <option value="基本合格">基本合格</option>
                    <option value="不合格">不合格</option>
                    <option value="未定等次">未定等次</option>
                    <option value="不考核">不参加考核</option>
                @{{#  } }}
            @{{#  } else { }}
                @{{#  if(d.assessment.length > 0 && d.assessment[0].situation_set.length > 0){ }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '优秀' && d.teaching_year > 0){ }}
                    <option value="优秀" selected='selected'>优秀</option>
                    @{{#  } else { }}
                    <option value="优秀">优秀</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '记功' && d.teaching_year > 0){ }}
                    <option value="记功" selected='selected'>优秀-记功</option>
                    @{{#  } else { }}
                    <option value="记功">优秀-记功</option>
                    @{{#  } }}
                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '嘉奖' && d.teaching_year > 0){ }}
                    <option value="嘉奖" selected='selected'>优秀-嘉奖</option>
                    @{{#  } else { }}
                    <option value="嘉奖">优秀-嘉奖</option>
                    @{{#  } }}

                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '合格' && d.teaching_year > 0){ }}
                    <option value="合格" selected='selected'>合格</option>
                    @{{#  } else { }}
                    <option value="合格">合格</option>
                    @{{#  } }}

                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '基本合格' && d.teaching_year > 0){ }}
                    <option value="基本合格" selected='selected'>基本合格</option>
                    @{{#  } else { }}
                    <option value="基本合格">基本合格</option>
                    @{{#  } }}

                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '不合格' && d.teaching_year > 0){ }}
                    <option value="不合格" selected='selected'>不合格</option>
                    @{{#  } else { }}
                    <option value="不合格">不合格</option>
                    @{{#  } }}

                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '未定等次' || d.teaching_year <= 0){ }}
                    <option value="未定等次" selected='selected'>未定等次</option>
                    @{{#  } else { }}
                    <option value="未定等次">未定等次</option>
                    @{{#  } }}

                    @{{#  if(d.assessment[0].situation_set[0].selectValue == '不考核' && d.teaching_year > 0){ }}
                    <option value="不考核" selected='selected'>不参加考核</option>
                    @{{#  } else { }}
                    <option value="不考核">不参加考核</option>
                    @{{#  } }}
                @{{#  } else { }}
                    <!-- 优秀-记功, 优秀-嘉奖, 合格, 基本合格, 不合格, 未定等次, 不参加考核（长病假*4） -->
                    @{{#  if(d.teaching_year > 0){ }}
                    <option value="合格" selected='selected'>合格</option>
                    @{{#  } else { }}
                    <option value="合格">合格</option>
                    @{{#  } }}
                    <option value="优秀">优秀</option>
                    <option value="记功">优秀-记功</option>
                    <option value="嘉奖">优秀-嘉奖</option>
                    <option value="基本合格">基本合格</option>
                    <option value="不合格">不合格</option>
                    @{{#  if(d.teaching_year <= 0){ }}
                    <option value="未定等次" selected='selected'>未定等次</option>
                    @{{#  } else { }}
                    <option value="未定等次">未定等次</option>
                    @{{#  } }}
                    <option value="不考核">不参加考核</option>
                @{{#  } }}
            @{{#  } }}
        </select>
    </script>

    <script type="text/html" id="opDemo">
        @{{#  if(d.user_id){ }}
        <a btnPermission="205" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" href="{{route('archives.index')}}?user_id=@{{d.user_id}}">查看</a>
        @{{#  } else { }}
        <a btnPermission="205" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" lay-event="tip">查看</a>
        @{{#  } }}
    </script>
</div>

@endsection
